using AhpilyServer;
using GameServer.Concurrent;
using GameServer.Model;
using System.Collections.Generic;

namespace GameServer.Cache
{
    public class AccountCache
    {
        /// <summary>
        /// 账号和模型的映射
        /// </summary>
        private Dictionary<string, AccountModel> accModelDict = new Dictionary<string, AccountModel>();
        /// <summary>
        /// 账号 与 客户端的映射(在线账号)
        /// </summary>
        private Dictionary<string, ClientPeer> accClientDict = new Dictionary<string, ClientPeer>();
        /// <summary>
        /// 客户端 与 账号的映射(在线账号)
        /// </summary>
        private Dictionary<ClientPeer, string> clientAccDict = new Dictionary<ClientPeer, string>();
        /// <summary>
        /// 线程安全的int类型
        /// </summary>
        private ConcurrentInt id = new ConcurrentInt(-1);
        /// <summary>
        /// 判断账号是否存在
        /// </summary>
        public bool IsExist(string account)
        {
            return accModelDict.ContainsKey(account);
        }
        /// <summary>
        ///  创建新账号
        /// </summary>
        public void Create(string account, string password)
        {
            
            AccountModel accountModel = new AccountModel(id.Add_Get(), account, password);
            accModelDict.Add(account, accountModel);
        }
        /// <summary>
        /// 获取账号对应的模型
        /// </summary>
        public AccountModel GetModel(string account)
        {
            // 不存在
            if(false == IsExist(account))
            {
                return null;
            }
            return accModelDict[account];
        }
        /// <summary>
        /// 账号密码是否匹配
        /// </summary>
        public bool IsMatch(string account, string password)
        {
            AccountModel model = GetModel(account);
            if (model == null)
                return false;
            return password == model.Password;
        }
        /// <summary>
        /// 是否在线
        /// </summary>
        public bool IsOnline(string account)
        {
            return accClientDict.ContainsKey(account);
        }
        /// <summary>
        /// 是否在线
        /// </summary>
        public bool IsOnline(ClientPeer client)
        {
            return clientAccDict.ContainsKey(client);
        }
        /// <summary>
        /// 上线
        /// </summary>
        public void Online(ClientPeer client, string account)
        {
            accClientDict.Add(account, client);
            clientAccDict.Add(client, account);
        }
        /// <summary>
        /// 下线
        /// </summary>
        public void Offline(ClientPeer client)
        {
            string account = clientAccDict[client];
            accClientDict.Remove(account);
            clientAccDict.Remove(client);
        }
        /// <summary>
        /// 下线
        /// </summary>
        public void Offline(string account)
        {
            ClientPeer client = accClientDict[account];
            accClientDict.Remove(account);
            clientAccDict.Remove(client);
        }

        /// <summary>
        /// 获取玩家的id
        /// </summary>
        public int GetId(ClientPeer client)
        {
            string account = clientAccDict[client];
            return GetId(account);
        }
        /// <summary>
        /// 获取玩家的id
        /// </summary>
        public int GetId(string account)
        {
            AccountModel model = accModelDict[account];
            return model.Id;
        }
    }
}
